package com.ohuang.onlytest.floating;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;


class FloatingMagnetHelper {

    Activity activity;

    FrameLayout frameLayout;

    FloatingMagnetView floatingMagnetView;


    boolean isShow = false;
    boolean isInit = false;

    ViewBuild viewBuild;

    public FloatingMagnetHelper(Activity activity, ViewBuild viewBuild) {
        this.activity = activity;
        this.viewBuild = viewBuild;
    }


    @SuppressLint("UseCompatLoadingForDrawables")
    void init() {
        frameLayout = new FrameLayout(activity);
        floatingMagnetView = new FloatingMagnetView(activity);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        View view = viewBuild.onBuild(activity);
        if (view != null) {
            floatingMagnetView.addView(view);
        }
        frameLayout.addView(floatingMagnetView, layoutParams);

        floatingMagnetView.setMagnetViewListener(new FloatingMagnetView.MagnetViewListener() {
            @Override
            public void onUp(FloatingMagnetView floatingMagnetView) {

            }

            @Override
            public void onClick(FloatingMagnetView floatingMagnetView) {
                if (viewBuild != null) {
                    viewBuild.onClick(activity);
                }
            }

            @Override
            public void onDown(FloatingMagnetView floatingMagnetView) {

            }

            @Override
            public void onRemove(FloatingMagnetView floatingMagnetView) {

            }
        });
        isInit = true;
    }

    public void show() {
        if (!isInit) {
            init();
            isInit = true;
        }
        if (!isShow) {
            isShow = true;
            View decorView = activity.getWindow().getDecorView();
            if (decorView instanceof ViewGroup) {
                ((ViewGroup) decorView).addView(frameLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                decorView.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mxy== null){
                            int x=decorView.getWidth()/2;
                            int y=decorView.getHeight()/2;
                            int[] xy=new int[2];
                            xy[0]=x;
                            xy[1]=y;
                            setXY(xy);
                        }
                    }
                });
            }
        }
    }

    public void hide() {
        if (!isInit) {
            return;
        }
        if (isShow) {
            isShow = false;
            View decorView = activity.getWindow().getDecorView();
            if (decorView instanceof ViewGroup) {
                ((ViewGroup) decorView).removeView(frameLayout);
            }
        }
    }

    public int[] getXY() {
        int[] xy = new int[2];
        xy[0] = (int) floatingMagnetView.getX();
        xy[1] = (int) floatingMagnetView.getY();
        return xy;
    }
    int[] mxy;

    public void setXY(int[] xy) {
        mxy = xy;
        floatingMagnetView.setX(xy[0]);
        floatingMagnetView.setY(xy[1]);
    }
}
